<?php
    function DBConnect()
    {
      global $db_host, $db_usr, $db_pass;
      mysql_connect($db_host, $db_usr, $db_pass) or die(mysql_error()); 
      global $db_name;
      mysql_select_db($db_name) or die(mysql_error());
    }
    
    function ownerCheck($img_id, $uid)
    { 
      global $sid; 
      global $gal_tbl;
      $query = "SELECT owner FROM $gal_tbl where id='$img_id'";
      $res = mysql_query($query, $sid);
      $a = mysql_fetch_array($res);
      
      if ($a[0] == $uid)
      {
         return true;
      }
      return false;
    }
    
    function SetSession($username, $userid)
    {
      global $_SESSION;
      $_SESSION['usr'] = $username;
      $_SESSION['uid'] = $userid;
    }
    
    function DestroySession()
    {
      global $_SESSION;
      unset($_SESSION['usr']);
      unset($_SESSION['uid']);
      session_destroy();
    }
    
    function isLogin()
    {
      global $_SESSION;
      if (isset($_SESSION['uid']))
      {
        return true;
      }
      return false;
    }
    
    function CreateThumbnail($img, $width, $height)
    {
      $source = "images/gallery/$img";
      echo $source;
      list($w, $h) = getimagesize($source);
      $newImage = imagecreatetruecolor($width, $height);
      $imageTmp = imagecreatefromjpeg($source);
      imagecopyresampled($newImage, $imageTmp, 0, 0, 0, 0, $width, $height, $w, $h);  
      
      $filename = "./images/gallery/thumbnails/$img";
       
      if (preg_match("*\.png/",$img))   
      {
        imagepng($newImage,$filename);
      } 
      else 
      {
		  imagejpeg($newImage,$filename);
      }
      
    	imagedestroy($imageTmp); 
    	imagedestroy($newImage); 
    }
    
    
    function getFileExtension($filename)
    {
      return substr($filename, strpos($filename, '.'));
    }
    
    function getFileName($filename)
    {
      return substr($filename, 0, strpos($filename, '.'));
    }
?>